﻿using IM.Easy.AuthorizationServices.Constants;
using IM.Easy.AuthorizationServices.Dtos;
using IM.Easy.AuthorizationServices.IServices;
using IM.Easy.Core.AppContext;
using IM.Easy.Core.Cache;
using IM.Easy.Core.Extensions;
using IM.Easy.LogServices.IServices;
using IM.Easy.MsgServices.IServices;
using IM.Easy.SystemServices.IServices;
using Mapster;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using SqlSugar;

namespace IM.Easy.Api.AccountManage
{
    public class AccountService
    {
        private readonly IUserService userService;
        private readonly IPermissionService permissionService;
        private readonly ISessionLogService sessionLogService;
        private readonly IExternalUserAccountService externalUserAccountService;
        public AccountService(IUserService userService
            , IPermissionService permissionService
            , ISessionLogService sessionLogService
            , IExternalUserAccountService externalUserAccountService)
        {
            this.userService = userService;
            this.permissionService = permissionService;
            this.sessionLogService = sessionLogService;
            this.externalUserAccountService = externalUserAccountService;
        }
        /// <summary>
        /// 获取当前账户信息
        /// </summary>
        /// <returns></returns>
        public AccountInfo GetCurrent()
        {
            var contextUser = ImApp.User;
            if (contextUser.Id == 0)
            {
                return new AccountInfo();
            }
            var key = $"{LoginConst.LoginUser}:{contextUser.Id}";
            var account = CacheManager.Get<AccountInfo>(key);
            if (account == null)
            {
                var user = userService.GetById(contextUser.Id);
                account = user.Adapt(account);
                permissionService.BindAccountPermission(account);
                permissionService.BindAccountRole(account);
                permissionService.BindFeature(account);
                CacheUser(account);
            }
            BindIpInfo(account);
            return account;
        }

        /// <summary>
        /// 绑定ip相关信息
        /// </summary>
        private void BindIpInfo(AccountInfo account)
        {
            var ipInfo = ImApp.Current.GetRemoteIpInfo();
            var address = $"{ipInfo.Country} {ipInfo.Province} {ipInfo.City}";
            account.IpInfo = new IpInfo { Address = address, Ip = ipInfo.IpAddress };
        }

        /// <summary>
        /// 登录
        /// </summary>
        public AccountInfo SignIn()
        {
            var contextUser = ImApp.User;
            var user = userService.GetById(contextUser.Id);
            var account = user.Adapt<AccountInfo>();
            sessionLogService.AddLoginLog(account.Id, account.UserName);
            permissionService.BindAccountPermission(account);
            permissionService.BindAccountRole(account);
            permissionService.BindFeature(account);
            CacheUser(account);
            return account;
        }

        /// <summary>
        /// 注销登录
        /// </summary>
        public void SignOut()
        {
            var account = GetCurrent();
            ClearCacheUser(account);
            if (account.Id > 0)
            {
                sessionLogService.AddLogoutLog(account.Id, account.UserName);
            }
        }


        /// <summary>
        /// 缓存当前账号数据
        /// </summary>
        /// <param name="account"></param>
        public void CacheUser(AccountInfo account)
        {
            CacheManager.Set($"{LoginConst.LoginUser}:{account.Id}", account);
        }

        /// <summary>
        /// 清除当前账号缓存
        /// </summary>
        /// <param name="account"></param>
        public void ClearCacheUser(AccountInfo account)
        {
            CacheManager.Remove($"{LoginConst.LoginUser}:{account.Id}");
        }
    }
}
